// File Name: step_motor_logic.c
// Author: kukukukuku~
// Date: 2024-06-28

#include "./User/Step_Motor/inc/step_motor_logic.h"
#include "./User/WS2812B/inc/ws2812b_ctrl.h"

/**
 * @brief   Step_Motor停止
 * @param   None
 * @retval  None
 * @note    None
*/
void step_motor_stop(void)
{
    STEP_MOTOR_DISA;
    tmr_counter_enable(STEP_MOTOR_PUL_TMR, FALSE);
    
    ws2812bState = 0;
}


/**
 * @brief   Step_Motor正转
 * @param   None
 * @retval  None
 * @note    只有控制Step_Motor状态发生改变时才能调用
*/
void step_motor_cw(void)
{
    STEP_MOTOR_ENA;
    STEP_MOTOR_DIR_CW;
    tmr_counter_enable(STEP_MOTOR_PUL_TMR, FALSE);
    tmr_counter_value_set(STEP_MOTOR_PUL_TMR, 0);
    tmr_counter_enable(STEP_MOTOR_PUL_TMR, TRUE);
    
    ws2812bState = 1;
}


/**
 * @brief   Step_Motor反转
 * @param   None
 * @retval  None
 * @note    只有控制Step_Motor状态发生改变时才能调用
*/
void step_motor_ccw(void)
{
    STEP_MOTOR_ENA;
    STEP_MOTOR_DIR_CCW;
    tmr_counter_enable(STEP_MOTOR_PUL_TMR, FALSE);
    tmr_counter_value_set(STEP_MOTOR_PUL_TMR, 0);
    tmr_counter_enable(STEP_MOTOR_PUL_TMR, TRUE);
    
    ws2812bState = 2;
}


/**
 * @brief   Step_Motor设置模式
 * @param   mode:   Step_Motor模式
 * @retval  None
 * @note    None
*/
void step_motor_set_mode(StepMotorMode mode)
{
    switch(mode)
    {
        case FULL_STEP:
            STEP_MOTOR_MS1_L;
            STEP_MOTOR_MS2_L;
            STEP_MOTOR_MS3_L;
            break;
        case HALF_STEP:
            STEP_MOTOR_MS1_H;
            STEP_MOTOR_MS2_L;
            STEP_MOTOR_MS3_L;
            break;
        case QUARTER_STEP:
            STEP_MOTOR_MS1_L;
            STEP_MOTOR_MS2_H;
            STEP_MOTOR_MS3_L;
            break;
        case EIGHTH_STEP:
            STEP_MOTOR_MS1_H;
            STEP_MOTOR_MS2_H;
            STEP_MOTOR_MS3_L;
            break;
        case SIXTEENTH_STEP:
            STEP_MOTOR_MS1_H;
            STEP_MOTOR_MS2_H;
            STEP_MOTOR_MS3_H;
            break;
        default:
            STEP_MOTOR_MS1_L;
            STEP_MOTOR_MS2_L;
            STEP_MOTOR_MS3_L;
            break;
    }
    
    stepMotorMode = mode;
}


/**
 * @brief   Step_Motor设置转速
 * @param   speedLevel: 转速档位(0~4), 0最高, 4最低
 * @retval  None
 * @note    None
*/
void step_motor_set_rotate_speed(uint8_t speedLevel)
{
    switch(speedLevel)
    {
        case 0:
            tmr_period_value_set(STEP_MOTOR_PUL_TMR, 1000);
            tmr_channel_value_set(STEP_MOTOR_PUL_TMR, STEP_MOTOR_PUL_TMR_CH, 500);
            break;
        case 1:
            tmr_period_value_set(STEP_MOTOR_PUL_TMR, 1500);
            tmr_channel_value_set(STEP_MOTOR_PUL_TMR, STEP_MOTOR_PUL_TMR_CH, 750);
            break;
        case 2:
            tmr_period_value_set(STEP_MOTOR_PUL_TMR, 2000);
            tmr_channel_value_set(STEP_MOTOR_PUL_TMR, STEP_MOTOR_PUL_TMR_CH, 1000);
            break;
        case 3:
            tmr_period_value_set(STEP_MOTOR_PUL_TMR, 2500);
            tmr_channel_value_set(STEP_MOTOR_PUL_TMR, STEP_MOTOR_PUL_TMR_CH, 1250);
            break;
        case 4:
            tmr_period_value_set(STEP_MOTOR_PUL_TMR, 3000);
            tmr_channel_value_set(STEP_MOTOR_PUL_TMR, STEP_MOTOR_PUL_TMR_CH, 1500);
            break;
        default:
            tmr_period_value_set(STEP_MOTOR_PUL_TMR, 3000);
            tmr_channel_value_set(STEP_MOTOR_PUL_TMR, STEP_MOTOR_PUL_TMR_CH, 1500);
            break;
    }
    
    stepMotorSpd = speedLevel;
}
